<?php

define('ACC', true);
require ('../includes/init.php');
$goods = new GoodsModel();

if($_GET['act'] == 'list'){
	
	require('../library/Fenye.class.php');
	$fenye = new Fenye();
	
	$rowCount = $goods->getRowCount();
	$pageSize = 10;
	
	$fenye->setMessage($rowCount, $pageSize);
	$pageCount = $fenye->getPageCount();
	
	if($pageCount <= 5){
		$fenye->setMaxPageSize($pageCount);
	}else{
		$fenye->setMaxPageSize(5);
	}
	
	if(isset($_GET['pageNow'])){
		$pageNow = $_GET['pageNow'] + 0;
		if($pageNow <= 0){
			$pageNow = 1;
		}else if($pageNow > $pageCount){
			$pageNow = $pageCount;
		}
	}else{
		$pageNow = 1;
	}
	
	$fenye->setPageNow($pageNow);
	
	$url = 'goods.php?act=list';
	$navigate = $fenye->getFenyeCode($url);
	
	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);

	$goods_list = $goods->getList($pageNow, $pageSize);

	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);
	$smarty->assign('goods_list', $goods_list);
	$smarty->assign('title', '商品列表');
	$smarty->assign('url', 'goods.php?act=add');
	$smarty->assign('link', '添加新商品');
	
	$smarty->display('./admin/goods_list.html');
	
}else if($_GET['act'] == 'search'){
	
	if(isset($_GET['sea']) && $_GET['sea'] == 'sea'){
		
		$cat_id = $_POST['cat_id'] + 0;
		$is_on_sale = $_POST['is_on_sale'] + 0;
		$keywords = trim($_POST['keywords']);
		$recommend = trim($_POST['recommend']);

		if($cat_id == 0 && $is_on_sale == 2 && $keywords == '' && $recommend == ''){
			header("Location: goods.php?act=list");
		}

		if($cat_id != 0){
			$arr[] = 'cat_id=' . $cat_id;
		}
		if($is_on_sale != 2){
			$arr[] = 'is_on_sale=' . $is_on_sale;
		}
		if($keywords != ''){
			$arr[] = "keywords like '%$keywords%'";
		}
		if($recommend !=''){
			$arr[] = "$recommend=1";
		}
		
		$where = '';
		foreach($arr as $key => $val){
			$where .= $val . ' and ';
		}
		$where = substr($where, 0, -4);
		
	}
	
	require('../library/Fenye.class.php');

	$rowCount = $goods->getRowCount2($where);
	$pageSize = 10;

	$fenye = new Fenye($rowCount, $pageSize);
	$pageCount = $fenye->getPageCount();
	
	
	$cat = new catModel();
	$list = $cat->getList();
	$category_list = $cat->catSort($list);
	
	$smarty->assign('category_list', $category_list);
	
	
	if(!isset($_POST['id'])){
		if(empty($_GET['pageNow'])){
			$pageNow = 1;
		}else{
			$pageNow = intval($_GET['pageNow']);
		}
	}else{
		$pageNow = intval($_POST['id']);
		if($pageNow > $pageCount){
			$pageNow = 1;
		}
	}
	$fenye->setPageNow($pageNow);
	
	$url = 'goods.php?act=search';
	$navigate = $fenye->getFenyeCode($url);
	$goods_list = $goods->getList2($pageNow, $pageSize, $where);

	$smarty->assign('action', 'search');
	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);
	$smarty->assign('goods_list', $goods_list);
	$smarty->assign('title', '商品列表');
	$smarty->assign('url', 'goods.php?act=add');
	$smarty->assign('link', '添加新商品');
	
	$smarty->display('./admin/goods_list.html');
	
}else if($_GET['act'] == 'add'){
	
	$catModel = new CatModel();
	$list = $catModel->getList();
	$category_list = $catModel->catSort($list);
	
	$merModel = new MerModel();
	$merList = $merModel->merList();

	$smarty->assign('merList', $merList);
	$smarty->assign('category_list', $category_list);
	$smarty->assign('title', '添加新商品');
	$smarty->assign('url', 'goods.php?act=list');
	$smarty->assign('link', '商品列表');
	$smarty->assign('action', 'insert');
	
	$smarty->display('./admin/goods_add.html');
	
}else if($_GET['act'] == 'insert'){
	
	require (ROOT.'library/Upload.class.php');
	require (ROOT.'library/image.class.php');

	$data = array();
	$data['goods_name'] = trim($_POST['goods_name']);
	$data['cat_id'] = $_POST['cat_id'] + 0;
	$data['mer_id']=$_POST['mer_id'] + 0;
	$data['shop_price'] = $_POST['shop_price'] + 0.00;
	$data['market_price'] = $_POST['market_price'] + 0.00;
	$data['goods_desc'] = trim($_POST['goods_desc']);
	$data['goods_number'] = $_POST['goods_number'] + 0;
	$data['is_best'] = isset($_POST['is_best'])?1:0;
	$data['is_new'] = isset($_POST['is_new'])?1:0;
	$data['is_hot'] = isset($_POST['is_hot'])?1:0;
	$data['is_on_sale'] = isset($_POST['is_on_sale'])?1:0;
	$data['keywords'] = trim($_POST['keywords']);
	$data['goods_brief'] = trim($_POST['goods_brief']);
	$data['add_time'] = time();
	
	$upload=new Upload();

	$res=$upload->up('ori_img');
	
	if(!$res){
		echo '文件上传失败！';
		$upload->getErr();
		exit;
	}
	
	$data['ori_img']=$res;
	
	if(!empty($data['ori_img'])){
		$ori_img = '';
		$ori_img = ROOT .$data['ori_img'];
		
		//$thumb = image::make_thumb($ori_img,190,145);
		$thumb = image::make_thumb($ori_img,220,220);
	
		if($thumb) {
			$data['goods_thumb'] = $thumb;
		}
	}

	if($goods_id=$goods->add($data)){
		$art['article_number'] = 'jd'.rand(100000,1000000).'_'.$goods_id;
		$where = "goods_id = '$goods_id'";
		if($goods->update($art, $where)){
			$desc = '发布' . $goods_id . '号商品成功！';
			$url = 'goods.php?act=list';
			$link = '商品列表';
			admin_msg($smarty, $desc, $url, $link);
		}else{
			echo '增加商品编号失败！';
		}
	}else{
		echo '发布失败！';
	}
	
}else if($_GET['act'] == 'edit'){
	$cat = new catModel();
	
	if($goods_id = ($_GET['goods_id']+0)){
		$row = $goods->getRow($goods_id);
	}else{
		echo '参数有误！';
		exit;
	}
	
	$arr = $cat->getListInfo();
	$cat_list = $cat->catSort($arr);
	for($i=0,$len=count($cat_list); $i<$len; $i++){
		$str = '';
		for($j=0,$len=$cat_list[$i]['lev']; $j<$len; $j++){
			$str .= '&nbsp;';
		}
		$cat_list[$i]['lev'] = $str;
	}

	$merList = $goods->getMerList();
	
	$cat_id = $goods->get_cat_id($goods_id);
	$cat_row = $goods->get_cat_row($cat_id);
	$attr_list = $goods->get_attr_list($cat_id);
	
	$cat_list2 = $goods->get_cat_list();

	$smarty->assign('cat_list2', $cat_list2);
	$smarty->assign('cat_row', $cat_row);
	$smarty->assign('attr_list', $attr_list);
	$smarty->assign('merList', $merList);
	$smarty->assign('row', $row);
	$smarty->assign('cat_list', $cat_list);
	$smarty->assign('title', '修改商品');
	$smarty->assign('url', 'goods.php?act=list');
	$smarty->assign('link', '商品列表');
	$smarty->assign('action', 'update');
	
	$smarty->display('./admin/goods_update.html');
	
}else if($_GET['act'] == 'update'){

	require (ROOT.'library/Upload.class.php');
	require (ROOT.'library/image.class.php');

	$data['goods_name'] = trim($_POST['goods_name']);
	$data['cat_id'] = $_POST['cat_id'] + 0;
	$data['mer_id']=$_POST['mer_id'] + 0;
	$data['shop_price'] = $_POST['shop_price'] + 0.00;
	$data['market_price'] = $_POST['market_price'] + 0.00;
	$data['goods_number'] = $_POST['goods_number'] + 0;
	$data['is_best'] = isset($_POST['is_best'])?1:0;
	$data['is_new'] = isset($_POST['is_new'])?1:0;
	$data['is_hot'] = isset($_POST['is_hot'])?1:0;
	$data['is_on_sale'] = isset($_POST['is_on_sale'])?1:0;
	$data['keywords'] = trim($_POST['keywords']);
	$goods_id = $_POST['goods_id']+0;

	$arr2 = $_POST;
	foreach($arr2 as $key => $val){
		if(is_numeric($key)){
			$arr[$key] = $val;
		}
	}

	if(trim($_POST['goods_desc'])){
		$data['goods_desc'] = trim($_POST['goods_desc']);
	}
	if(trim($_POST['goods_brief'])){
		$data['goods_brief'] = trim($_POST['goods_brief']);
	}
	
	if(!$data['goods_name']){
		echo '商品名不能为空！';
		exit;
	}

	$upload=new Upload();
	if(isset($_FILES['ori_img'])){
		$res=$upload->up('ori_img');
		
		$data['ori_img']=$res;
		
		if(!empty($data['ori_img'])){
			$ori_img = '';
			$ori_img = ROOT .$data['ori_img'];
		
			//$thumb = image::make_thumb($ori_img,190,145);
			$thumb = image::make_thumb($ori_img,220,220);
		
			if($thumb) {
				$imgs = $goods->get_img_path($goods_id);
				
				$file_path = ROOT . $imgs['goods_thumb'];
				if(is_file($file_path)){
					if(!(unlink($file_path))){
						echo '删除图片error!';
					}
				}else{
					echo '图片文件不存在！';
				}
					
				$file_path = ROOT . $imgs['ori_img'];
				if(is_file($file_path)){
					if(!(unlink($file_path))){
						echo '删除图片error!';
					}
				}else{
					echo '图片文件不存在！';
				}
				
				$data['goods_thumb'] = $thumb;
			}
		}
	}

	$where = 'goods_id = '. $goods_id;
		
	if($goods->update($data, $where)){
		if(isset($arr) && $arr != ''){
			
			foreach($arr as $key=>$val){
			
				if($goods->get_attr_id($key, $goods_id)){
					if($val != 0){
						$arr4['attr_id'] = $key;
						$arr4['goods_id'] = $goods_id;
						$arr4['attr_value'] = $val;
							
						//$arr4['attr_price'] = $attr_price;
							
						if(!($goods->goods_attr_update($arr4, 'goods_attr', 'update', "attr_id = '$arr4[attr_id]' and goods_id = $goods_id"))){
							echo '修改失败！';
							return;
						}
					}
			
			
				}else{
					$arr4['attr_id'] = $key;
					$arr4['goods_id'] = $goods_id;
					$arr4['attr_value'] = $val;
					//$arr4['attr_price'] = $attr_price;
			
					if(!($goods->goods_attr_insert($arr4, 'goods_attr'))){
						echo '修改失败！';
						return;
					}
				}
			
			}
		}
		$desc = '修改 ' . $data['goods_name'] . ' 商品成功！';
		$url = 'goods.php?act=list';
		$link = '商品列表';
		admin_msg($smarty, $desc, $url, $link);
	
	}else{
		echo '修改失败！';
	}
	
}else if($_GET['act'] == 'delete' || $_GET['act'] == 'rollback'){
	
	$goods_id=$_GET['id']+0;
	$good_name = $goods->getGoodName($goods_id);
	
	if($_GET['act'] == 'delete'){
		if($goods->delete($goods_id)){
			echo 1;
		}else{
			echo 4;
		}
	}else{
		if($goods->rollback($goods_id)){
			echo 1;
		}else{
			echo 4;
		}
	}
	
}else if($_GET['act'] == 'attr_oper'){
	
	$cat_id = $_GET['cat_id'];
	$attr_list = $goods->get_attr_list($cat_id);
	
	echo json_encode($attr_list);
	return;
	
}



?>